package com.example.ware.service.impl;

import com.common.to.es.SkuEsUpTo;
import com.common.to.es.SkuHasStockTo;
import com.common.utils.R;
import com.example.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.example.ware.dao.WmsWareSkuDao;
import com.example.ware.entity.WmsWareSkuEntity;
import com.example.ware.service.WmsWareSkuService;

import javax.validation.constraints.DecimalMax;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Autowired
    private WmsWareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                new QueryWrapper<WmsWareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageBySkuIdAndWare(Map<String, Object> params) {
        String wareid = (String) params.get("wareId");
        //skuId
        String skuid = (String) params.get("skuId");
        QueryWrapper<WmsWareSkuEntity> wrapper = new QueryWrapper<>();
        if (wareid != null && wareid.length() > 0) {
            wrapper.eq("ware_id", Long.parseLong(wareid));
        }
        if (skuid != null && skuid.length() > 0) {
            wrapper.eq("sku_id", Long.parseLong(skuid));
        }
        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1、判断如果还没有这个库存记录新增
        List<WmsWareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WmsWareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (entities == null || entities.size() == 0) {
            WmsWareSkuEntity skuEntity = new WmsWareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            // 远程查询sku的名字，如果失败，整个事务无需回滚
            //1、自己catch异常
            // 还可以用什么办法让异常出现以后不回滚？高级
            try {
                R info = productFeignService.info(skuId);
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");

                if (info.getCode() == 0) {
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {

            }


            wareSkuDao.insert(skuEntity);
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }

    }

    @Override
    public List<SkuHasStockTo> hasStock(List<Long> skuIds) {
        ArrayList<SkuHasStockTo> list = new ArrayList<>();
        for (Long skuId : skuIds) {
            //查出一个数字
            Integer stockNum = wareSkuDao.hasStock(skuId);
            if (stockNum != null && stockNum > 0) {
                SkuHasStockTo hasStockTo = new SkuHasStockTo();
                hasStockTo.setStock(stockNum);
                hasStockTo.setSkuId(skuId);
                list.add(hasStockTo);
            }
        }
        return list;
    }
}